import { Popup, XDialog, XInput, XButton, Group } from 'vux'

let baseUrl = "http://iot.malanco.com/";
//let baseUrl = "http://localhost:8080/";
let data = {
    showLoginDialog: false,

    //cameraUrl: "http://223.113.15.72/6977CF7E756318166A958520C1/030008110250D62FA0F60A05CF07DDDB7FDF99-AC4D-2AFB-090A-F6FFE693C6DC.mp4?ccode=0502&duration=399&expire=18000&psid=25d9abb47431261842a2aa919c500d7c&sp=&ups_client_netip=7016e86b&ups_ts=1532160399&ups_userid=&utid=y0hSE7oSj2gCAXAW6IzjBF%2Fj&vid=XNDUxNDcxMTU2&vkey=B7119e9499a8acee64c372e2eb0f8784d&s=4c6a9a68bce811e0bf93",
    cameraUrl: null,
    errorMsg: null,
    username: null,
    password: null
};

let errorToastMsg = {
    login_error_too_many_times: "登录失败次数太多，请30分钟后再试",
    password_too_simple: "密码太简单",
    unExistDevice: "摄像头在天翼平台尚未绑定",
    server_error: "天翼内部服务错误",
    not_exist_device: "天翼平台不存在次摄像头，请先在天翼平台绑定摄像头",
    device_not_online: "摄像头不在线，请先让摄像头上线"
};

let errorLoginMsg = {
    invalid_account: "天翼平台不存在此账户，请重新登录",
    invalid_password: "密码失效，请重新登录",
    login_error_too_many_times: "请先登录天翼账户",
    invalid_session: "请重新登录"
};

let methods = {
    getCameraUrl() {
        this.$http.get(baseUrl + "getCameraUrl").then((res) => {
            let data = res.data;

            if (data.success == "true") {
                this.cameraUrl = data.data;
                return;
            }
            let errorCode = data.exceptionObj.code;
            if (errorCode == 'not_exist_device') {
                this.$vux.toast.show({
                    text: "未绑定摄像头,请先到设备绑定界面绑定摄像头",
                    type: "text"
                });
                return;
            }

            var tianyiPrefixIndex = errorCode.indexOf("tianyi.");
            // 电信摄像头
            if (tianyiPrefixIndex == 0) {
                var tianyiErrorCode = errorCode.substr(7);

                if (errorToastMsg[tianyiErrorCode]) {
                    this.$vux.toast.show({
                        text: errorToastMsg[tianyiErrorCode],
                        type: "text"
                    });
                    return;
                }

                if (errorLoginMsg[tianyiErrorCode]) {
                    this.errorMsg = errorLoginMsg[tianyiErrorCode];
                    this.showLoginDialog = true;
                    return;
                }
                this.$vux.toast.show({
                    text: "未知错误",
                    type: "text"
                });
                return;

            }
        });
    },

    doLogin() {
        if (this.username === null || this.password === null) {
            this.$vux.toast.show({
                text: "用户名或密码不能为空",
                type: "warn"
            });
            return;
        }

        let url = baseUrl + "tianyiLogin";
        let params = {
            "account": this.username, "password": this.password
        };
        this.$http.post(url, params).then((res) => {
            let data = res.data;

            if (data.success == "true") {
                location.reload();
            } else {
                var loginErrorCode = data.exceptionObj.code;
                if (loginErrorCode == 'tianyi.invalid_account' || loginErrorCode == 'tianyi.invalid_password') {
                    if (loginErrorCode == 'tianyi.invalid_password') {
                        this.errorMsg = "密码不正确，请重新登录";
                    } else {
                        this.errorMsg = "用户不存在，请重新登录";
                    }
                } else if (loginErrorCode == 'tianyi.login_error_too_many_times') {
                    this.errorMsg = "登录失败次数太多，请30分钟后再试";
                } else if (loginErrorCode == 'tianyi.login_error_too_many_times') {
                    this.errorMsg = "密码太简单";
                } else if (loginErrorCode == 'tianyi.unExistDevice') {
                    this.errorMsg = "摄像头在天翼平台尚未绑定";
                } else {
                    this.errorMsg = "天翼内部服务错误";
                }
            }
        });


    }
};

export default {
    destroyed() {
        this.showLoginDialog = false;
        this.cameraUrl = null;
        this.errorMsg = null;
        this.username = null;
        this.password = null;
    },
    mounted() {
        this.getCameraUrl();
    },
    methods: methods,
    data: function () {
        return data;
    },
    components: {
        Popup,
        XDialog,
        XInput,
        XButton,
        Group
    }
}
